package com.cqc.mybatis01.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

/**
 * 使用JDBC操作数据库
 * @author dell
 *
 */
public class JDBCDemo {

	@Test
	public void test1() {
		/**
		 * 1 加载驱动
		 * 2 获取连接
		 * 3 编写sql，预编译sql，赋值
		 * 4 实行sql
		 * 5 遍历结果集
		 * 6 释放资源
		 */
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet resultSet=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "root", "root");
			String sql="select * from user where username=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, "王五");
			resultSet = ps.executeQuery();
			while (resultSet.next()) {
				int id=resultSet.getInt("id");
				String username=resultSet.getString("username");
				System.out.println(id+"---"+username);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
